package com.content.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.content.pojo.Article;
import com.content.pojo.ArticleClass;
import com.content.pojo.ArticleClassExample;
import com.content.pojo.BelongtoKey;
import com.content.pojo.User;
import com.content.service.ArticleClassService;
import com.content.service.ArticleService;
import com.content.service.belongToService;
import com.content.util.ObjectToJson;
import com.sun.mail.iap.Response;


@Controller
@RequestMapping("/ArticleClass")
public class ArticleClassController {

	@Autowired
	private ArticleClassService aService;
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private belongToService bService;
	/*
	 * 这个是测试control**/
	@RequestMapping("/template-management/articleMngTpl")
	public String findMangeArticleMngTpl(){
		return "template-management/articleMngTpl";
	}
	
	/*
	 * 这个是测试control**/
	@RequestMapping("/template-management/indexTpl")
	public String findMangeIndexTpl(){
		return "template-management/indexTpl";
	}
	
	/*
	 * 这个是测试control**/
	@RequestMapping("/template-management/recordsTpl")
	public String findMangeRecordsTpl(){
		return "template-management/recordsTpl";
	}
	/*
	 * 这个是测试control**/
	@RequestMapping("/template-management/nav")
	public String findMangeNav(){
		return "template-management/nav";
	}
	
	/*
	 * 这个是测试control**/
	@RequestMapping("/template-management/systembackupsTpl")
	public String findMangeSystembackupsTpl(){
		return "template-management/systembackupsTpl";
	}
	
	/*
	 * 这个是测试control**/
	@RequestMapping("/indexManagement")
	public String findIndexManagement(){
		return "index/indexManagement";
	}
	
	
	/*
	 * 获取文章分类信息**/
	@RequestMapping(value="classify",method = RequestMethod.GET) 
	public  void getAllClass(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		List<ArticleClassExample> aList = aService.getAllClasses();
		
		JSONArray array = new JSONArray();
		
		for (ArticleClassExample articleClassExample : aList){
			array.add(JSON.toJSON(articleClassExample));
		}
		
		//System.out.println(aList.get(0).getTitle());
		PrintWriter out = response.getWriter();
		out.print(array);
		//return aList;
	}

	
	/*
	 * 获取文章信息**/
	@RequestMapping(value="findarticle",method = RequestMethod.GET)
	public void getAllArticle(HttpServletRequest request,HttpServletResponse response) throws IOException{
        
		List<Article> aList = articleService.getAllArticle();
		
		System.out.println(aList.size());
		
		JSONArray array = new JSONArray(); 
		  	
		for (Article article : aList){

			array.add(JSON.toJSON(article));
		}

		PrintWriter out = response.getWriter();
		out.print(array);
	}
	

	/*
	 * 获取用户信息**/
	@RequestMapping(value="finduser",method = RequestMethod.GET)
	public void getAllUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
        
		List<User> aList = aService.getAllUser();
		
		JSONArray array = new JSONArray();
		
		for (User user : aList){
			array.add(JSON.toJSON(user));
		}
		
		//System.out.println(aList.get(0).getTitle());
		PrintWriter out = response.getWriter();
		out.print(array);
	}
	/*删除分类**/
	@RequestMapping(value="deleteclass",method = RequestMethod.POST)
	public void deleteClass(String class_id, HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();
		//System.out.println(class_id);
		try{
		    aService.deleteClass(class_id);
		    out.print(1);
		}catch(Exception e){
			out.print(-1);
		}
		
		
	}
	/*删除文章审核信息**/
	@RequestMapping(value="deletearticle",method = RequestMethod.POST)
	public void deleteArticle(String article_id, String class_id,HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();
        System.out.println("我是测试");
        BelongtoKey belongtoKey = new BelongtoKey();
		belongtoKey.setClassId(class_id);
		belongtoKey.setArticleId(article_id);
		belongtoKey.setIsBoolean(1);

		aService.passCheck(belongtoKey);
		out.print(1);

		
		
	}
	
	/*文章通过审核**/
	@RequestMapping(value="passCheck",method = RequestMethod.POST)
	public void passCheck(String articleId, String classId, HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();
		
		BelongtoKey belongtoKey = new BelongtoKey();
		belongtoKey.setClassId(classId);
		belongtoKey.setArticleId(articleId);
		belongtoKey.setIsBoolean(1);

		aService.passCheck(belongtoKey);
		out.print(1);
	}
	
	/*文章拒绝发布**/
	@RequestMapping(value="reject",method = RequestMethod.POST)
	public void reject(String articleId, String classId, HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();
		
//		BelongtoKey belongtoKey = new BelongtoKey();
//		belongtoKey.setClassId(classId);
//		belongtoKey.setArticleId(articleId);
//		belongtoKey.setIsBoolean(1);
//
//		aService.passCheck(belongtoKey);
		out.print(1);
	}
}
